/**
 * Created by songj on 2015/12/11.
 */
define(['angular',
    'framework/msg/GillionMsgModule',
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/datasource/DataSourceModule',
    'framework/dropdown/GillionDropdownModule',
    'framework/date/DateModule',
    'angular-underscore'
], function (angular) {
    var module = angular.module('PublishResultController',
        ['GillionMsgModule', 'DataGridModule', 'GillionPaginationModule',
            'DataSourceModule', 'DateModule', 'GillionDropdownModule'])
        .controller('PublishResultController', ['$scope', 'GillionMsgService', '$dataSourceManager', '$timeout', 'Arrays', "$location",'Resource','GillionMsg','$compile','$http','$compile',
                function ($scope, GillionMsgService, $dataSourceManager, $timeout, Arrays, $location,Resource,GillionMsg,$compile) {
                    $scope.responseLog = GillionMsgService.getInputData();
                    $scope.logShow=true;
                    $scope.results = $scope.responseLog.result.data.result;
                    $scope.search = {
                        responseLog:  $scope.responseLog
                    };
                    $scope.showLog = function(){
                        if($scope.logShow){
                            $scope.logShow=false;
                            $("#logDetail").css("height","0px");
                            $("#img").attr("class","icon-engine-upward-b");
                            window.resizeTo(500, 100);
                        }else{
                            $scope.logShow=true;
                            $("#logDetail").css("height","180px");
                            $("#img").attr("class","icon-engine-down-b");
                            window.resizeTo(500, 300);
                        }
                    };


                    $timeout(function () {
                        var logDetail = $scope.responseLog.result.success ? $scope.responseLog.result.data.mes : $scope.responseLog.result.data.mes,
                            lines = logDetail.split("\n"),
                            lineCount = lines.length,
                            $logDetail = angular.element("#logDetail");
                        $scope.logDetail = "";
                        GillionMsgService.setOutputData($scope.responseLog.result.data.packageId);
                        var printCount = 0;

                        /*var print = function (lineBegin, lineEnd) {
                            return function () {
                                var printLines = lines.slice(lineBegin, lineEnd);
                                angular.forEach(printLines, function (line) {
                                    $scope.logDetail += line + "\n";
                                });
                                $logDetail.scrollTop( $logDetail[0].scrollHeight);
                            };
                        };
                        var i = 0;
                        while (printCount < lineCount) {
                            var printLine = Math.random() * 10;
                            var lineBegin = printCount;
                            var lineEnd = printCount + printLine;
                            printCount = printCount + printLine;
                            $timeout(print(lineBegin, lineEnd), 100 * (i++));
                        }*/
                        $scope.logDetail=logDetail;

                        $timeout(function () {
                            if(!$scope.responseLog.result.success){
                                angular.element("#mes").text(
                                    "发布失败！"
                                );
                            }
                            else if($scope.responseLog.result.success){
                                angular.element("#mes").html(
                                    "<img src='../../static/images/cg.png' style='vertical-align:middle;padding-right:10px;'>发布成功！"
                                );
                            }
                            /*$scope.showClear = true;
                            var list =$scope.responseLog.result.result;
                            var flag = false;
                            for(var i =0;i<list.length;i++){
                                if(!list[i].success){
                                    flag=true;
                                }
                            }
                            if (!flag) {
                                angular.element("#mes").html(
                                    "<img src='../../static/images/cg.png' style='vertical-align:middle;padding-right:10px;'>发布成功！"
                                );
                            } else {

                            }*/
                        }, 300*(i++));
                    },100);
                }
            ]
        );

    module.config(['$locationProvider', function($locationProvider) {
        $locationProvider.html5Mode(true);
    }]);
    return module;
});
